package com.ymatou.iapi.productquery.compare;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.json.JSONException;

import com.ymttest.utils.FileUtil;
import com.ymttest.utils.YMTDateUtil;

public class MainTest {
	private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(30, 60, Long.MAX_VALUE,
			TimeUnit.SECONDS, new LinkedBlockingQueue<>());

	public static void runTask(List<Runnable> runList) {
		Date startDate = YMTDateUtil.getDate();

		runList.forEach(ele -> {
			threadPoolExecutor.execute(ele);
		});

		threadPoolExecutor.shutdown();// 只是不能再提交新任务，等待执行的任务不受影响

		try {
			boolean loop = true;
			do { // 等待所有任务完成
				loop = !threadPoolExecutor.awaitTermination(1, TimeUnit.SECONDS); // 阻塞，直到线程池里所有任务结束
			} while (loop);

		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		Date endDate = YMTDateUtil.getDate();
		System.out.println((endDate.getTime() - startDate.getTime()) / 1000);
		System.out.println(endDate);
	}

	public static <T> List<Runnable> compareApiAsList(List<T> prodOrCatalogIds, Consumer<List<T>> function){
		List<Runnable> runList = new ArrayList<>();
		for (int i = 0; i < prodOrCatalogIds.size(); i++) {
			List<T> tmp = new ArrayList<>();
			tmp.add(prodOrCatalogIds.get(i));
			runList.add(() -> {
				function.accept(tmp);
			});
		}
		return runList;

	}
	
	public static <T,V> List<Runnable> compareApiAsList(List<T> prodOrCatalogIds, V isTrade,BiConsumer<List<T>, V> function) {

		List<Runnable> runList = new ArrayList<>();
		for (int i = 0; i < prodOrCatalogIds.size(); i++) {
			List<T> tmp = new ArrayList<>();
			tmp.add(prodOrCatalogIds.get(i));
			runList.add(() -> {
				function.accept(tmp, isTrade);
			});
		}
		return runList;
	}
	
	public static <T> List<Runnable> compareApiAsPer(List<T> prodOrCatalogIds, Consumer<T> function){
		List<Runnable> runList = new ArrayList<>();
		for (int i = 0; i < prodOrCatalogIds.size(); i++) {
			T tmp = prodOrCatalogIds.get(i);
			runList.add(() -> {
				function.accept(tmp);
			});
		}
		return runList;

	}
	
	public static <T,V> List<Runnable> compareApiAsPer(List<T> prodOrCatalogIds, V isTrade,BiConsumer<T, V> function) {

		List<Runnable> runList = new ArrayList<>();
		for (int i = 0; i < prodOrCatalogIds.size(); i++) {
			T tmp = prodOrCatalogIds.get(i);
			runList.add(() -> {
				function.accept(tmp, isTrade);
			});
		}
		return runList;
	}
	

	public static <T> List<List<T>> divideList(List<T> list, int groupNum){
		List<List<T>> retList = new ArrayList<List<T>>();
		
		int size = (list.size()) / groupNum;
		int size2 = (list.size()) % groupNum;

		int j = 0;
		int xx=0;
		for (int i = 0; i < groupNum; i++) {
			List<T> tmpList = new ArrayList<>();
			for (j = xx; j < (size + xx); j++) {
				tmpList.add(list.get(j));
			}
			xx = j;
			retList.add(tmpList);
		}
		if (size2 != 0) {
			List<T> tmpList = new ArrayList<>();
			for (int y = 1; y < size2 + 1; y++) {
				tmpList.add(list.get(list.size() - y));
			}
			retList.add(tmpList);
		}
		return retList;
	}
	
	public static void main(String[] args) throws JSONException {
		String dataPath = "D:\\compare\\data\\";
		
		
		FileUtil.deleteFile(LoggerHelper.logPath);
		FileUtil.deleteFile(LoggerHelper.diffPath);
		FileUtil.deleteFile(LoggerHelper.errorPath);
		FileUtil.deleteFile(LoggerHelper.syclogPath);
		
		
		String catalogPath = dataPath +"catalogId.txt";
		String prodPath = dataPath + "productId.txt";
		String sellerIdPath = dataPath + "sellerId.txt";
		String liveIdPath = dataPath + "liveId.txt";
		String acitvityProdIdPath = dataPath + "acitvityProdId.txt";
		
		List<String> catalogs = FileUtil.readFileAsLine(catalogPath);
		List<String> products = FileUtil.readFileAsLine(prodPath);
		List<String> sellerIds = FileUtil.readFileAsLine(sellerIdPath);
		List<String> liveIds = FileUtil.readFileAsLine(liveIdPath);
		List<String> acitvityProdIds = FileUtil.readFileAsLine(acitvityProdIdPath);
		
		
		List<Runnable> runList = new ArrayList<>();
		if (args.length>0){
			String[] params = args[0].split("-");
			
			for(int i=0;i<params.length;i++){
				String k = params[i];
				if (k.equals("1")){
					runList.addAll(compareApiAsPer(sellerIds, x -> SearchCompare.compareProductInListForHot(x)));
				}
				
				if (k.equals("2")){
					runList.addAll(compareApiAsPer(sellerIds, x -> SearchCompare.compareProductInListForNew(x)));
				}
				
				if (k.equals("3")){
					runList.addAll(compareApiAsList(sellerIds, x -> SearchCompare.compareRecommendProductList(x)));
				}
				
				if (k.equals("4")){
					runList.addAll(compareApiAsPer(liveIds, x -> SearchCompare.compareTopProductInLive(x)));
				}
			
				if (k.equals("5")){
					acitvityProdIds.forEach(ele->{
						String[] eleAry = ele.split(",");
						runList.add(()->SearchCompare.compareSecKillProductActivityStock(eleAry[1], eleAry[0]));
					});
				}
				
				if (k.equals("6")){
					runList.addAll(compareApiAsPer(products,  x -> SearchCompare.compareProductDescExtra(x)));
				}

				if (k.equals("7")){
					runList.addAll(compareApiAsPer(products, true, ( x, y) -> SearchCompare.compareGetProductInfo(x, y)));
				}
				
				if (k.equals("8")){
					runList.addAll(compareApiAsList(products, true, (x, y) -> SearchCompare.compareGetProductInList(x, y)));
				}
			
				if (k.equals("9")){
					catalogs.forEach(catalog->runList.add(()->SearchCompare.compareGetCatalogListByDeliveryExtra(Arrays.asList(catalog,"3"), true)));
				}
				
				if (k.equals("Tong")){
					
					products.forEach(ele->SearchCompareHelper.callSyc(ele));
				}
					
				

			}
			
		}else{
			//e625e4ba-7643-4fe5-a43d-8e2d211bb58e
		}
	
		runTask(runList);
	}
	
	
	
}
